"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 故障&可靠性测试
Case Name   : 目录无权限	双机场景：主机创建表空间时所需目录无权限
Create At   : 2020/07/02
@zou_jialiang0505328114
Description :
    1、启动数据库
    2、删除目录权限
    3、主机使用无权限目录创建表空间
    4、恢复目录权限为700
    5、创建表空间
Expect      :
    1、执行成功
    2、执行成功
    3、执行失败
    4、执行成功
    5、执行成功
History     :
    2020/12/7 n@ningyali 1、优化脚本；2、teardown中增加主备机对表、表空间及表空间路径的删除
"""

import unittest

from yat.test import Node
from yat.test import macro

from testcase.utils.CommonSH import *


class Deletaduit(unittest.TestCase):
    sh_primy = CommonSH('PrimaryDbUser')
    sh_standby = CommonSH('Standby1DbUser')
    nodes_tuple = ('PrimaryDbUser', 'Standby1DbUser', 'Standby2DbUser')
    Constant = Constant()

    @RestartDbCluster(*nodes_tuple)
    def setUp(self):
        self.log = Logger()
        self.log.info('------------Opengauss_Reliability_Dbsys_Case027.py start------------')
        self.primary_dbuser = Node(node='PrimaryDbUser')
        self.primary_root = Node(node='PrimaryRoot')
        self.standby1_root = Node(node='Standby1Root')
        self.standby1_dbuser = Node(node='Standby1DbUser')
        self.standby2_root = Node(node='Standby2Root')
        self.path = os.path.join(os.path.dirname(macro.DB_INSTANCE_PATH), 'dbsys027_tablespace')

    def test_startdb(self):
        self.log.info('-------------------step2： 删除表空间目录权限----------------------')
        self.log.info(self.path)
        mkdircmd = 'mkdir -m 000 -p ' + self.path
        self.log.info(mkdircmd)
        mkdirresult = self.primary_dbuser.sh(mkdircmd).result()
        self.log.info(mkdirresult)

        self.log.info('-------------------step3:主机使用无权限目录创建表空间----------------------')
        createdb = f'''drop tablespace if exists dbsys027_tbl;create tablespace dbsys027_tbl location '{self.path}';'''
        createdbresult = self.sh_primy.executDbSql(createdb)
        self.log.info(createdb)
        self.log.info(createdbresult)
        self.assertIn(self.Constant.PERMISSION_DENY_MSG, createdbresult)

        self.log.info('-------------------step4:恢复目录权限为700----------------------------')
        self.primary_dbuser.sh('chmod 700 ' + self.path)
        self.log.info('chmod 700 ' + self.path)

        self.log.info('-------------------step5：创建表空间-----------------------')
        createdbresult = self.sh_primy.executDbSql(createdb)
        self.log.info(createdb)
        self.log.info(createdbresult)
        self.assertIn('CREATE TABLESPACE', createdbresult)

    def tearDown(self):
        self.log.info("--------------------this is tearDown--------------------")
        self.log.info("--------------------drop tablespace--------------------")
        cmd = f'''drop tablespace if exists dbsys027_tbl;'''
        msg = self.sh_primy.executDbSql(cmd)
        self.log.info(cmd)
        self.log.info(msg)

        self.log.info("--------------------delete file-------------------")
        shell_cmd = f"rm -rf {self.path}"
        msg = self.primary_root.sh(shell_cmd).result()
        self.log.info(msg)
        msg = self.standby1_root.sh(shell_cmd).result()
        self.log.info(msg)
        msg = self.standby2_root.sh(shell_cmd).result()
        self.log.info(msg)
